package org.jl.main.util;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class UtilForObject {

    private static final DateTimeFormatter formatterDatetime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter formatterTime = DateTimeFormatter.ofPattern("HH:mm:ss");

    private static final ThreadLocal<SimpleDateFormat> threadLocalFormatter =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    /*
    Ojbect方法
     */
    public static boolean isEmpty(Object obj) {
        return ObjectUtils.isEmpty(obj);
    }

    public static boolean isNotEmpty(Object obj) {
        return ObjectUtils.isNotEmpty(obj);
    }

    public static boolean isEqual(@NonNull Object a, Object b) {
        return switch (a) {
            case Boolean ignored -> Boolean.valueOf((boolean) a).equals(b);
            case Character ignored -> Character.valueOf((char) a).equals(b);
            case Byte ignored -> Byte.valueOf((byte) a).equals(b);
            case Short ignored -> Short.valueOf((short) a).equals(b);
            case Integer ignored -> Integer.valueOf((int) a).equals(b);
            case Long ignored -> Long.valueOf((long) a).equals(b);
            case Float ignored -> Float.valueOf((float) a).equals(b);
            case Double ignored -> Double.valueOf((double) a).equals(b);
            case BigDecimal ignored -> BigDecimal.valueOf((double) a).equals(b);
            default -> false;
        };
    }

    /*
    String方法
     */
    //如果为空返回结果不正确
    public static boolean isNoneEmpty(@NonNull String... str) {
        return StringUtils.isNoneEmpty(str);
    }

    public static boolean isBlank(String str) {
        return StringUtils.isBlank(str);
    }

    public static boolean isNotBlank(String str) {
        return StringUtils.isNotBlank(str);
    }

    public static boolean isNoneBlank(@NonNull String... str) {
        return StringUtils.isNoneBlank(str);
    }

    public static String defaultValue(String str, String defaultValue) {
        return StringUtils.defaultIfEmpty(str, defaultValue);
    }

    /*
    Boolean方法
     */
    public static boolean isTrue(Boolean bool) {
        return Boolean.TRUE.equals(bool);
    }

    public static boolean isFalse(Boolean bool) {
        return Boolean.FALSE.equals(bool);
    }

    /*
    日期、时间方法
     */
    @Deprecated
    public static String format(Date date) {
        return threadLocalFormatter.get().format(date);
    }

    @Deprecated
    public static Date parse(String dateStr) {
        try {
            return threadLocalFormatter.get().parse(dateStr);
        } catch (ParseException e) {
//            throw new RuntimeException(e);
            return null;
        }
    }

    public static String formatForDateTime(LocalDateTime localDateTime) {
        return localDateTime.format(UtilForObject.formatterDatetime);
    }

    public static String formatForDate(LocalDateTime localDateTime) {
        return localDateTime.format(UtilForObject.formatterDate);
    }

    public static String formatForTime(LocalDateTime localDateTime) {
        return localDateTime.format(UtilForObject.formatterTime);
    }

    public static LocalDateTime parseForDateTime(String dateStr) {
        return LocalDateTime.parse(dateStr, UtilForObject.formatterDatetime);
    }

}